#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <vector>
using namespace std;


 // Definition for singly-linked list.
 struct ListNode {
    int val;
    ListNode *next;
    ListNode() : val(0), next(nullptr) {}
    ListNode(int x) : val(x), next(nullptr) {}
    ListNode(int x, ListNode *next) : val(x), next(next) {}
};
 
class Solution {
public:
    ListNode* reverseKGroup(ListNode* head, int k) {
        if (head == nullptr || head->next == nullptr) return head;
        int n = 0;
        ListNode* pcur = head;
        while (pcur)
        {
            n++;
            pcur = pcur->next;
        }
        int m = n / k, kn = k - 1;
        ListNode* newhead = new ListNode, * prev = head, * oldtail = head;
        pcur = head->next;
        while (m--)
        {
            if (newhead->next == nullptr)
            {
                while (kn--)
                {
                    ListNode* next = pcur->next;
                    pcur->next = prev;
                    prev = pcur;
                    pcur = next;
                }
                oldtail = head;
                newhead->next = prev;
                kn = k - 1;
            }
            else
            {
                ListNode* tail = pcur;
                prev = pcur;
                pcur = pcur->next;
                while (kn--)
                {
                    ListNode* next = pcur->next;
                    pcur->next = prev;
                    prev = pcur;
                    pcur = next;
                }
                kn = k - 1;
                oldtail->next = prev;
                oldtail = tail;
            }
        }
        oldtail->next = pcur;
        return newhead->next;
    }
};

int main()
{
    Solution s;
    int k;
    cin >> k;
    ListNode* head = new ListNode;
    ListNode* pcur = head;
    int n = 1;
    while (cin >> n)
    {
        ListNode* tmp = new ListNode(n);
        pcur->next = tmp;
        pcur = tmp;
    }
    s.reverseKGroup(head->next, k);
    return 0;
}

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* reverseKGroup(ListNode* head, int k) {
        ListNode* newhead = new ListNode;
        ListNode* prev = newhead, * pcur = head;

        int n = 0;
        while (pcur)
        {
            pcur = pcur->next;
            n++;
        }
        n /= k;
        ListNode* cur = head;
        for (int i = 0; i < n; i++)
        {
            ListNode* tmp = cur;
            for (int j = 0; j < k; j++)
            {
                ListNode* next = cur->next;
                cur->next = prev->next;
                prev->next = cur;
                cur = next;
            }
            prev = tmp;
        }
        prev->next = cur;
        cur = newhead->next;
        delete newhead;
        return cur;
    }
};